package zhihuilan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import zhihuilan.entity.MyAcquisition;

import java.util.List;

@Mapper
public interface MyAcquisitionMapper extends BaseMapper<MyAcquisition> {
    @Delete(" delete FROM my_acquisition_msg where pid = #{id}")
    Integer deleteMyAcquisitionMsg(@Param("id") Integer pid);

    @Select({" <script> select a.* from my_acquisition a " +
            " left join my_acquisition_msg b on b.pid=a.id \n" +
            " where a.status =1 \n" +
            " <when test='staffId != null and staffId != \"\"'> " +
            "  and b.staff_id = #{staffId} \n" +
            " </when> " +
            " <when test='keyword != null and keyword != \"\"'> " +
            " and (a.title like concat('%',#{keyword},'%') or a.hyaddress like concat('%',#{keyword},'%') or LOWER(CONVERT(a.content USING utf8)) like concat('%',#{keyword},'%')) " +
            " </when> " +
            " GROUP BY a.id " +
            " order by create_time desc LIMIT #{page},#{pageSize} " +
            " </script>"})
    List<MyAcquisition> selectMyAcquisitionPage(@Param("page") Long page, @Param("pageSize") Long pageSize, @Param("keyword") String keyword, @Param("staffId") Integer staffId);

    @Select({" <script> select count(1) from ( select a.* from my_acquisition a " +
            " left join my_acquisition_msg b on b.pid=a.id \n" +
            " where a.status =1 \n" +
            " <when test='staffId != null and staffId != \"\"'> " +
            "  and b.staff_id = #{staffId} \n" +
            " </when> " +
            " <when test='keyword != null and keyword != \"\"'> " +
            " and (a.title like concat('%',#{keyword},'%') or a.hyaddress like concat('%',#{keyword},'%') or LOWER(CONVERT(a.content USING utf8)) like concat('%',#{keyword},'%')) " +
            " </when> " +
            " GROUP BY a.id ) tab "+
            " </script>"})
    Integer selectMyAcquisitionCount(@Param("keyword") String keyword, @Param("staffId") Integer staffId);
}
